With If, Elif, and Else, we have entered the “Wow, I can do that with Python” arena! Learning about the different Python data types was cool, but it was hard to keep my attention. There are only so many times you can get excited about pulling back a value from a dictionary key or finding new ways to sort a list. It was a necessary evil, though. As programmers, we need to understand the different nuances of each data type, how to retrieve data, and how we can manipulate the objects stored in that data type. Understanding data types was the foundation of what was to come.
These next few blog posts will dive into the logic we can now add to our programs. We can start to solve more problems and have the computer do tedious and repetitive tasks. The first statement that we will look at If, Elif, and Else.
When you want to use flow control, there is a special syntax that we have to adhere to, namely colons (:) and white spaces. This is what makes Python different than other programming languages. The outline for flow control is:
if condition:
execute lines of code
elif another_condition:
execute these lines of code
else:
execute this code
You can have multiple elif statements but only one if / else in the same block. However, you can have another if/elif/else block under the else statement.
If / Elif / Else is a conditional statement, a condition has to be met for the script section to execute. I like to approach if I am going to use If/Elif/Else, do I want sections of code to fire if a condition returns true? To visualize this, when I drive my daughter to daycare, we play a game where we find people walking, and one of us shouts, “PEOPLE!”. If she finds one, she receives a point. When I find one, I receive a point. If we visualize this in a code block:
o_people_cnt = 0
d_people_cnt = 0
if (o_find_people):
o_people_cnt = o_people_cnt + 1
elif (d_find_people):
d_people_cnt = d_people_cnt + 1
else:
print('No People Found :')
Breaking down the above:
- if
o_find_people
is True- it will take the variable
o_people_cnt
and add one to it
- it will take the variable
- if
o_find_people
is False, it will go down to the elif (Else If) statement, - Else If
d_find_people
is True- it will take the variable
d_people_cnt
and add one to it
- it will take the variable
- if
d_find_people
is False, it will go down the catch-all else statement - if nothing is True, it will print ‘No People Found.’
There are a few things to note:
- On the statement lines (If/Elif), we could do:
if o_find_poeple == True
- We do not have to do this; we can use what is in the demo. When you write if / elif statements, it is assumed that it has to evaluate to True.
- We can also consolidate when we are reassigning the variable to the current value of the variable + 1
o_people_cnt = o_people_cnt + 1
- This can be changed to:
o_people_cnt += 1
- This is just a shorthand way of writing it.
- It works with any math operator.
Another example is to check to see if an input is greater than 10. If not, is it equal to 5? If none are true, print your number is not greater than 10 and is not equal to 5
user_input = int(input('Please enter a number: '))
if user_input > 10:
print(f'{user_input} is greater than 10!')
elif user_input == 5:
print(f'{user_input} is equal to 5!')
else:
print(f'{user_input} is not greater than 10 or equal to 5')
When we run the above code to validate:
Please enter a number: 15
15 is greater than 10!
This is just scratching the surface of what we can do with conditionals in Python.